﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebChamCong
{
    public partial class mana_office : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddl_Department.Items.Clear();
                ddl_department1.Items.Clear();
                Load_Department();
                Load_Department1();
                Loadten(int.Parse(ddl_Department.SelectedValue));
            }
        }
        protected void Load_Department()
        {
            List<DTO.BoPhanDTO> bp = BUS.BoPhanBUS.GetBoPhans();
            foreach (var obj in bp)
            {
                ddl_Department.Items.Add(new ListItem(obj.DepartmentName, obj.DepartmentID.ToString()));
            }
            ddl_Department.SelectedIndex = 0;
        }
        protected void Load_Department1()
        {
            List<DTO.BoPhanDTO> bp = BUS.BoPhanBUS.GetBoPhans();
            foreach (var obj in bp)
            {
                    ddl_department1.Items.Add(new ListItem(obj.DepartmentName, obj.DepartmentID.ToString()));
            }
            ddl_department1.SelectedIndex = 0;
        }
        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }
        protected void bttchange_Click(object sender, EventArgs e)
        {
            if(BUS.NhanVienBUS.ChangeDepartment(int.Parse(this.ddl_TenNV.SelectedValue),int.Parse(this.ddl_department1.SelectedValue)))
            {
                MessageBox("Thay đổi thành công !");
            }
        }
        protected void Loadten(int ID)
        {
            List<DTO.NhanVienDTO> nv = BUS.NhanVienBUS.GetNhanViensByDepartID(ID);
            foreach (var obj in nv)
            {
                ddl_TenNV.Items.Add(new ListItem(obj.FullName, obj.UserId.ToString()));
            }
            ddl_TenNV.SelectedIndex = 0;
        }
        protected void ddl_Department_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddl_TenNV.Items.Clear();
            List<DTO.NhanVienDTO> nv = BUS.NhanVienBUS.GetNhanViensByDepartID(int.Parse(ddl_Department.SelectedValue));
            if (nv.Count() == 0)
            {
                ddl_TenNV.Items.Add("Không có nhân viên");
            }
            else
            {
                foreach (var obj in nv)
                {
                    ddl_TenNV.Items.Add(new ListItem(obj.FullName, obj.UserId.ToString()));
                }
                ddl_TenNV.SelectedIndex = 0;
            }
        }
    }
}